﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlExtractor.Output;
using HtmlExtractor;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;

namespace SLHtmlExtractor.ObjectOutput
{
    /// <summary>
    /// 只支持属性
    /// 属性类型不能为nullable
    /// 集合属性必须有add方法
    ///  
    /// </summary>
    public class ConvertToObject
    {
        //public object OutputObject;
        #region IOutput<object> Members
        bool isNewComposite = false;
        bool isNewSimple = false;
        string simplePath = null;
        private static Dictionary<string, object> simpleCache = new Dictionary<string, object>();
        public bool TryGetOutPutResult(PageResult result,ref object outputObj)
        {
            if (result == null)
                return false;
            //web.book.chapter.name
            Type outputType=outputObj.GetType();
            foreach (IGrouping<List<string>, IPageItem> grouping in result.GroupByOutputPath())
            {
                if (grouping.Key.Count == 1)
                {
                    
                    foreach(SimpleItem s in grouping.Cast<SimpleItem>())
                    {
                        simplePath = s.OutputPath;
                        isNewSimple = true;
                        FindParent(s.OutputPath, s.Value,ref outputObj);
                    }
                }
                else
                {
                   // isComposite = true;
                    foreach (CompositeItem citem in grouping.Cast<CompositeItem>())
                    {
                        isNewComposite = true;
                        foreach (SimpleItem s in citem)
                        {                          
                            FindParent(s.OutputPath, s.Value,ref outputObj);
                            isNewComposite = false;
                        }
                        
                    }
                }
            }
            return true;
        }
        object tempComposite = null;
        /// <summary>
        /// 集合必须有add方法。不支持字典
        /// </summary>
        /// <param name="parentPath"></param>
        /// <param name="value"></param>
        /// <param name="obj"></param>
        private void FindParent(string parentPath,object value,ref object obj)
        {  
               
            if (parentPath.Split('.').Count() == 1)
            {
                Type itemType;
                if (ReflectorHelper.TryGetCollectionItemType(obj.GetType(), out itemType))
                {
                    object item=null;
                   
                    if (isNewComposite)
                    {
                        item = ReflectorHelper.CreateInstance(itemType);
                        ReflectorHelper.SetProperety(item, value, parentPath.Split('.').Single());
                        ReflectorHelper.CallMethod(obj, "Add", new object[] { item });
                        tempComposite = item;
                    }
                    else
                    {
                        if (isNewSimple==true&&tempComposite == null)
                        {
                            if (itemType == typeof(String))
                            {
                                ReflectorHelper.CallMethod(obj, "Add", new object[] { value });
                            }
                            else
                            {
                                item = ReflectorHelper.CreateInstance(itemType);
                                ReflectorHelper.SetProperety(item, value, parentPath.Split('.').Single());
                                ReflectorHelper.CallMethod(obj, "Add", new object[] { item });
                            }
                           
                        }
                        else
                        {
                            ReflectorHelper.SetProperety(tempComposite, value, parentPath.Split('.').Single());
                        }
                    }
                   
                }
                else
                {
                    ReflectorHelper.SetProperety(obj, value, parentPath.Split('.').Single());
                }
            }
            else
            {
                //todo 处理集合
                string propertyName = parentPath.Split('.').First();
                Object subobj=null;
                if (ReflectorHelper.GetProperty(obj, propertyName) == null)
                {
                    Type t = obj.GetType();
                    PropertyInfo pi = t.GetProperty(propertyName);
                    subobj = ReflectorHelper.CreateInstance(pi.PropertyType);
                    ReflectorHelper.SetProperety(obj, subobj, propertyName);
                    parentPath = parentPath.Remove(0, propertyName.Length + 1);
                }
                else
                {
                    subobj= ReflectorHelper.GetProperty(obj, propertyName);
                    parentPath = parentPath.Remove(0, propertyName.Length + 1);
                }
                FindParent(parentPath, value,ref subobj);
            }
        }
        #endregion
    }
}
